﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// ===============================================================================

using DevExpress.XtraEditors;
using System;
using System.Linq;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Filter;
using XCI.Sys.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    public partial class FrmRoleAuthorize : BaseForm
    {
        private ModuleHelper moduleHelper;
        private readonly SysRole _role;

        public FrmRoleAuthorize(SysRole role)
        {
            InitializeComponent();
            this._role = role;
        }

        private async void FrmRoleAuthorize_Load(object sender, EventArgs e)
        {
            this.Name = $"{_role.Name} - 功能模块授权";
            treeModule.ModelType = typeof(SysModule);
            treeDept.ModelType = typeof(SysDept);
            checkDept.Checked = true;
            moduleHelper = new ModuleHelper(treeModule);
            treeDept.ForceInitialize();

            //读取角色权限
            var rolePermissionResult = await SysRoleService.Instance.SelectRolePermissionAsync(_role.Id);

            //初始化权限模块
            var currentUserPermissionResult = await SysAccountService.Instance.SelectUserPermissionAsync(SysApp.CurrentUser.Id);
            treeModule.DataSource = currentUserPermissionResult.Modules;
            treeModule.SetCheckedNodeIds(rolePermissionResult.Modules.Select(p => p.Id).ToArray());
            await moduleHelper.InitAsync(moduleImage);
            moduleHelper.ExpandNode();

            //初始化数据权限
            treeDept.DataSource = await SysDeptService.Instance.SelectListAsync(new SysDeptFilter { Status = true, EnableDeptScope = true });
            var role = await SysRoleService.Instance.SelectByIdAsync(_role.Id);
            if (role.DeptScope == 2) treeDept.SetCheckedNodeIds(rolePermissionResult.CustomDepts.Select(p => p.Id).ToArray());//自定义
            WinFormHelper.SetRadioGroupCheckedValue(role.DeptScope, checkAll, checkCustom, checkDeptAndLower, checkDept, checkSelf);
            treeDept.ExpandAll();
        }

        private void checkDeptScope_CheckedChanged(object sender, EventArgs e)
        {
            var edit = (CheckEdit)sender;
            treeDept.Enabled = edit.Tag.ToInt() == 2;
        }

        private void TreeListx1_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
        {
            e.NodeImageIndex = moduleHelper.GetModuleImageIndex(e.Node);
        }

        private async void BtnSave_Click(object sender, EventArgs e)
        {
            var permission = new RolePermissionBody();
            permission.RoleIds = _role.Id;
            permission.DeptScope = WinFormHelper.GetRadioGroupCheckedValue(checkAll, checkCustom, checkDeptAndLower, checkDept, checkSelf).ToInt(4);
            permission.DeptIds = treeDept.CheckedIds;
            permission.ModuleIds = treeModule.CheckedIds;
            await SysRoleService.Instance.SaveRolePermissionAsync(permission);
            DialogOk();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogCancel();
        }
    }
}